package com.wjh.kt_vod.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wjh.kt_vod.model.vod.Course;
import com.wjh.kt_vod.model.vod.CourseDescription;
import com.wjh.kt_vod.model.vod.Subject;
import com.wjh.kt_vod.model.vod.Teacher;
import com.wjh.kt_vod.mapper.CourseMapper;
import com.wjh.kt_vod.service.*;
import com.wjh.kt_vod.vo.vod.CourseFormVo;
import com.wjh.kt_vod.vo.vod.CoursePublishVo;
import com.wjh.kt_vod.vo.vod.CourseQueryVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course>
        implements CourseService {

    @Autowired
    private TeacherService teacherService;

    @Autowired
    private SubjectService subjectService;

    @Autowired
    private ChapterService chapterService;

    @Autowired
    private VideoService videoService;

    @Autowired
    private CourseDescriptionService courseDescriptionService;


    @Override
    public Map<String, Object> findPageCourse(Page<Course> coursePage, CourseQueryVo courseQueryVo) {

        String title = courseQueryVo.getTitle();
        Long subjectId = courseQueryVo.getSubjectId();
        Long subjectParentId = courseQueryVo.getSubjectParentId();
        Long teacherId = courseQueryVo.getTeacherId();
        QueryWrapper<Course> courseQueryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(title)) {
            courseQueryWrapper.like("title", title);
        }
        if (!StringUtils.isEmpty(subjectId)) {
            courseQueryWrapper.eq("subject_id", subjectId);
        }
        if (!StringUtils.isEmpty(subjectParentId)) {
            courseQueryWrapper.eq("subject_parent_id", subjectParentId);
        }
        if (!StringUtils.isEmpty(teacherId)) {
            courseQueryWrapper.eq("teacher_id", teacherId);
        }
        Page<Course> coursePages = baseMapper.selectPage(coursePage, courseQueryWrapper);

        List<Course> records = coursePages.getRecords();
        records.stream().forEach(item -> {
            this.getNameById(item);
        });

        HashMap<String, Object> map = new HashMap<>();
        map.put("totalCount", coursePages.getTotal()); //总记录数
        map.put("totalPage", coursePages.getPages()); //总页数
        map.put("records", records); //数据
        return map;
    }


    private void getNameById(Course item) {
//        根据讲师id查询讲师名称
        Teacher teacher = teacherService.getById(item.getTeacherId());
        if (!StringUtils.isEmpty(teacher)) {
            String name = teacher.getName();
            item.getParam().put("teacherName", name);
        }
//         根据课程id查询获取课程分类名称
        Subject subject = subjectService.getById(item.getSubjectId());
        if (!StringUtils.isEmpty(subject)) {
            String title = subject.getTitle();
            item.getParam().put("subjectTitle", title);
        }

        Subject byId = subjectService.getById(item.getSubjectParentId());
        if (!StringUtils.isEmpty(subject)) {
            String title = byId.getTitle();
            item.getParam().put("subjectParentTitle", title);
        }
    }

    /**
     * 添加课程
     *
     * @return
     */
    @Override
    public Long saveCourseInfo(CourseFormVo courseFormVo) {
        //添加课程基本信息表 course
        Course course = new Course();
        BeanUtils.copyProperties(courseFormVo, course);
        baseMapper.insert(course);

        CourseDescription courseDescription = new CourseDescription();
        courseDescription.setCourseId(course.getId());

        //添加课程描述信息
        courseDescriptionService.save(courseDescription);

        return course.getId();
    }

    /**
     * 根据id查询方法
     * @param id
     * @return
     */
    @Override
    public CourseFormVo getCourseFormVoById(Long id) {
        Course course = baseMapper.selectById(id);
        if (StringUtils.isEmpty(course)) {
            return null;
        }
        CourseDescription byId = courseDescriptionService.getById(id);
        CourseFormVo courseFormVo = new CourseFormVo();
        BeanUtils.copyProperties(course,courseFormVo);
        if (!StringUtils.isEmpty(byId)){
            courseFormVo.setDescription(byId.getDescription());
        }
        return courseFormVo;
    }


    /**
     * 根据id修改课程信息
     * @param courseFormVo
     */
    @Override
    public void updateCourseById(CourseFormVo courseFormVo) {

        Course course = new Course();
        BeanUtils.copyProperties(courseFormVo,course);
        baseMapper.updateById(course);

        CourseDescription courseDescription = new CourseDescription();

        courseDescription.setCourseId(courseFormVo.getId());
        courseDescription.setDescription(courseDescription.getDescription());
        courseDescription.setId(course.getId());
        courseDescriptionService.updateById(courseDescription);
    }

    @Override
    public CoursePublishVo getCoursePublishVo(Long id) {
        return baseMapper.selectCoursePublishVoById(id);
    }

    /**
     * 课程最终发布
     * @param id
     */
    @Override
    public void publishCourse(Long id) {
        Course course = baseMapper.selectById(id);
        course.setStatus(1);
        course.setUpdateTime(new Date());
        baseMapper.updateById(course);
    }

    @Override
    public void removeCourseById(Long id) {

//        删除章节
        chapterService.removeChapterByCourseId(id);
//        删除小节
        videoService.removeVideoByCourseId(id);
//        删除描述
        courseDescriptionService.removeDescriptionByCourseId(id);
//        删除课程
        baseMapper.deleteById(id);
    }


}




